//获取后台的用户值，展示在页面
$(function () {
    var addBooks = JSON.parse(localStorage.getItem("addBooks"));
    var string = template("demo", { addBooks: addBooks })
    document.getElementById('tb').innerHTML = string;
});

//新增
function doAdd() {
    //新增图书，
    layui.use("layer", function () {
        var layer = layui.layer;
        newOne = layer.open({
            type: 1,
            content: $('#msg'),
            area: ['500px', '650px'],
            btn: ['新增', '关闭'],
            yes: function (index, layero) {
                //按钮【按钮一】的回调
                var addBooks = JSON.parse(localStorage.getItem("addBooks"));//取出的字符串转换成数组

                var obj = {};
                obj.id = addBooks.length + 1;
                obj.title = $("#addBookForm input:eq(0)").val();//title

                obj.author = $("#addBookForm input:eq(1)").val();//author
                obj.publishing = $("#addBookForm input:eq(2)").val();//publishing
                obj.img = $("#uploadPic")[0].files[0].name;//img路径（要用dom属性）


                addBooks.push(obj);
                addBooks = JSON.stringify(addBooks);
                localStorage.setItem('addBooks', addBooks);
                layer.close(newOne);
                location.reload();

            }
        });
    })
}


//显示模态框中的图片
var flog = false;
function picShow(obj) {
    $("#showImg")[0].src = window.URL.createObjectURL(obj.files[0]);
    //（要用dom属性）
    flog = true;
}
//删除全部
function doDelAll() {
    $("#tb").children().remove();
    var addBooks = JSON.parse(localStorage.getItem("addBooks"));//取出的字符串转换成数组
    addBooks.length = 1;
    addBooks = JSON.stringify(addBooks);
    localStorage.setItem('addBooks', addBooks);
    location.reload();
}


//删除单行,(后台数据一并删除)
function doDel(obj) {
    var addBooks = JSON.parse(localStorage.getItem("addBooks"));//取出的字符串转换成数组
    var tds = $(obj).parent().parent().children();
    var index = tds.eq(0).html();//找到当前的序号
    for (var i in addBooks) {
        if (addBooks[i].id == index) {
            addBooks.splice(i, 1);
            $(obj).parent().parent().remove();//删除当前行 
        }

    }

    addBooks = JSON.stringify(addBooks);
    localStorage.setItem('addBooks', addBooks);
}

//修改,(已修改后台),未校验admin
function doChange(t) {
    layui.use('layer', function () {
        flog = false;
        var layer = layui.layer;
        var tds = $(t).parent().parent().children();
        $("#addBookForm input:eq(0)").val(tds[1].innerHTML);
        $("#addBookForm input:eq(1)").val(tds[2].innerHTML);
        $("#addBookForm input:eq(2)").val(tds[3].innerHTML);
        //模态框里的标题作者出版社修改栏
        var add = $(tds[4]).children().attr("src");//找到当前点击的这一行的图片路径
        console.log(add)
        $("#showImg").attr("src", add);//将路径放到模态框里进行显示

        var name = $("#addBookForm input:eq(0)").val().trim();//找到当前书籍的标题

        xiugai = layer.open({
            type: 1,
            content: $('#msg'),
            area: ['500px', '700px'],
            btn: ['修改', '关闭'],
            yes: function (index, layero) {
                //按钮【按钮一】的回调

                var obj = {};
                var addBooks = JSON.parse(localStorage.getItem("addBooks"));//取出的字符串转换成数组
                obj.title = $("#bookname").val();
                obj.author = $("#author").val();
                obj.publishing = $("#publishing").val();

                for (let i in addBooks) {
                    if (addBooks[i].title == name) {
                        if (flog) {
                            obj.img = $("#uploadPic")[0].files[0].name;
                        } else {
                            obj.img = addBooks[i].img;

                        }//将之前找到的白体与后台库里进行对比，一致后将这一行进行替换
                        
                        addBooks[i] = obj;//将obj对象加入后台
                    }
                }
                addBooks = JSON.stringify(addBooks);//转换成字符串
                localStorage.setItem('addBooks', addBooks);
                layer.close(xiugai);
                // location.reload();
            },

        })
    });

}

//查询
function doSearch(){
    flag=false;
    var addBooks = JSON.parse(localStorage.getItem("addBooks"));
    var inputInner=$("#search").val().trim();
    for(var i=0;i<addBooks.length;i++){
        if(inputInner==addBooks[i].title){
            flag=true;
            $("#tb").empty();
            var obj=[addBooks[i]];
            var string = template("demo", {addBooks:obj})
            document.getElementById('tb').innerHTML = string;
            
        }       
    }
    if(flag==false){
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg('不存在该本书');
            });
        }
}

